package com.yanyeori.framework.core.util;

import java.util.UUID;

/**
 * Id工具类
 *
 * @author chenkuan
 * @since 2022/11/11
 */
public class IdUtil {

    private static volatile SnowFlakeUtil snowFlakeUtil;
    private static final byte[] SNOW_FLAKE_LOCK = {1};

    /**
     * 获取UUID
     */
    public static String randomUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 获取去除短横线的UUID
     */
    public static String simpleUUID() {
        return randomUUID().replace("-", "");
    }

    /**
     * 获取一个单例的雪花对象，单机器唯一
     */
    public static SnowFlakeUtil getSnowFlake() {
        if (snowFlakeUtil == null) {
            synchronized (SNOW_FLAKE_LOCK) {
                if (snowFlakeUtil == null) {
                    snowFlakeUtil = new SnowFlakeUtil();
                }
            }
        }
        return snowFlakeUtil;
    }

    /**
     * 获取单例雪花Id，单机器唯一
     */
    public static long getSnowFlakeNextId() {
        return getSnowFlake().nextId();
    }
}
